package com.quanyan.vote.web;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.redisCluster.utils.SerializeUtil;
import com.quanyan.vote.cache.MyRedisTemplate;
import com.quanyan.vote.entity.vo.req.ReqVoteDetail;
import com.quanyan.vote.entity.vo.req.ReqVoteFirstPage;
import com.quanyan.vote.entity.vo.req.ReqVoteToExpert;
import com.quanyan.vote.entity.vo.resp.RespExactlyExpertDetail;
import com.quanyan.vote.mapper.TbVoteExpertMapper;
import com.quanyan.vote.service.TbVoteExpertService;
import com.quanyan.vote.utils.VoteEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Tuple;

import java.util.*;

/**
 * 用户投票接口
 * Created by xian-guo on 2016/6/8.
 */

@RestController
@RequestMapping(value = "api/vote")
public class TpUserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TpUserController.class);

    @Autowired
    private TbVoteExpertService tbVoteExpertService;

    @Autowired
    MyRedisTemplate redisTemplate;
    /**
     * 投票首页
     * @return
     */
    @RequestMapping(value = "voteFirstPage",method = RequestMethod.POST)
    public APIResponse<Map<String,Object>> voteFirstPage(@RequestBody ReqVoteFirstPage reqVoteFirstPage){
        LOGGER.debug("----voteFirstPage----->>>>>"+JSON.toJSONString(reqVoteFirstPage));
        if (null == reqVoteFirstPage || StringUtil.isBlankOrNull(reqVoteFirstPage.getD8bd9db46973e30cfa3413e9d78f6b97())){
            return APIResponse.returnFail(VoteEnum.PARAMETER_CANT_BE_EMPTY_FOR_VOTE);
        }
        return tbVoteExpertService.voteFirstPage(reqVoteFirstPage);
    }

    /**
     * 查看达人投票详情
     * @param reqVoteDetail
     * @return
     */
    @RequestMapping(value = "expertDetail",method = RequestMethod.POST)
    public APIResponse<RespExactlyExpertDetail> expertDetail(@RequestBody ReqVoteDetail reqVoteDetail){
        LOGGER.debug("----expertDetail----->>>>>"+JSON.toJSONString(reqVoteDetail));
        if (null == reqVoteDetail || StringUtil.isBlankOrNull(reqVoteDetail.getD8bd9db46973e30cfa3413e9d78f6b97())){
            return APIResponse.returnFail(VoteEnum.PARAMETER_CANT_BE_EMPTY_FOR_VOTE);
        }
        return tbVoteExpertService.expertDetail(reqVoteDetail);
    }

    /**
     * 投票给达人
     * @param reqVoteToExpert
     * @return
     */
    @RequestMapping(value = "voteToExpert",method = RequestMethod.POST)
    public APIResponse voteToExpert(@RequestBody ReqVoteToExpert reqVoteToExpert){
        LOGGER.debug("----voteToExpert----->>>>>"+JSON.toJSONString(reqVoteToExpert));
        if (null == reqVoteToExpert){
            return APIResponse.returnFail(VoteEnum.PARAMETER_CANT_BE_EMPTY_FOR_VOTE);
        }
        if (StringUtil.isBlankOrNull(reqVoteToExpert.getD8bd9db46973e30cfa3413e9d78f6b97()) || StringUtil.isBlankOrNull(reqVoteToExpert.getIp())
                || null == reqVoteToExpert.getId()){
            return APIResponse.returnFail(VoteEnum.PARAMETER_CANT_BE_EMPTY_FOR_VOTE);
        }
        return tbVoteExpertService.voteToExpert(reqVoteToExpert);
    }



    @Autowired
    private TbVoteExpertMapper tbVoteExpertMapper;
    @RequestMapping(value = "redisTest",method = RequestMethod.POST)
    public APIResponse redisTest(){
        System.out.println("---------redis--------------");
//        RedisKey rk = new RedisKey(RedisConstants.VOTE_FORWORD_CACHE_PREFIX, "xxg_vote");
//        TbVoteExpertExample tbVoteExpertExample=new TbVoteExpertExample();
//        List<TbVoteExpert> tbVoteExperts=tbVoteExpertMapper.selectByExample(tbVoteExpertExample);
//        redisTemplate.set(rk,tbVoteExperts);
//
        RedisKey rkv = new RedisKey(RedisConstants.VOTE_FORWORD_CACHE_PREFIX, "xxg_vote_vote");
//        Map<byte[],Double> map=new HashMap<byte[], Double>();
//        for(int i=0;i<tbVoteExperts.size();i++){
//            TbVoteExpert tbVoteExpert=tbVoteExperts.get(i);
//            map.put(SerializeUtil.serialize(tbVoteExpert.getId()),(double)tbVoteExpert.getVoteCount());
//        }
//        redisTemplate.zadd(rkv,map);//不会覆盖原数据，如果一样则覆盖

//        double d=redisTemplate.zIncr(rk,1,SerializeUtil.serialize(11));
//        TbVoteExpert tbVoteExpert= (TbVoteExpert) SerializeUtil.unserialize(tuple.getBinaryElement());
        int id=1;
//        double d=redisTemplate.zIncr(rkv,1,SerializeUtil.serialize(id));
//        System.out.println(d);
        Set<Tuple> set = redisTemplate.zrevrangeWithScores(rkv,0,10);
        System.out.println(set.size()+"----");
        int i=0;
        for (Tuple tuple:set){
            int ty=(int)SerializeUtil.unserialize(tuple.getBinaryElement());
                System.out.println("----"+tuple.getScore()+"-----");
            i++;
        }
        System.out.println(i);
        return APIResponse.returnSuccess();
    }

}
